package com.kitty.game.activity.service.boos;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.activity.model.product.SuperBoss;
import com.kitty.game.activity.service.boos.service.BoosService;
import com.kitty.game.activity.service.time.FightActivityHandler;
import com.kitty.game.boss.config.BossSet;
import com.kitty.game.boss.model.BossFightParam;
import com.kitty.game.boss.model.BossParam;
import com.kitty.game.boss.model.SuperBossParam;
import com.kitty.game.boss.model.SuperBossZPParam;
import com.kitty.game.common.BoosTitleDeal;
import com.kitty.game.config.NPC;
import com.kitty.game.enter.TitleInfo;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.listener.event.FightEndEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.*;


/**
 * 地府炎魔处理类
 */
@Component
public class DiFuYanMoHandler extends FightActivityHandler {

    @Autowired
    FightService fightService;
    private Map<String, SuperBoss> superBossMap = new HashMap<>();
    private static final String boy_name[] = {"烈焰滔天","钢筋铁骨","法力禁锢","欲火焚身"};

    /**
     * 最少组队人数
     */
    private static final int MIN_TEAM_COUNT = 1;
    private static final String TEAM_COUNT_NOT_ENOUGH = "你还是凑齐三个人再来向我挑战吧！[离开]";
    private static final int ACCEPT_MIN_LEVEL = 40;
    private static final String TEAM_LEVEL_NOT_ENOUGH = "#Y{0}#n的等级还没有{1}级。[离开]";

    @PostConstruct
    private void init() {
        SuperBoss superBoss = new SuperBoss("炼狱冥炎", 6510, 702);
        superBoss.setZPName("烈焰滔天");
        superBoss.getArtifactMap().put("混元金斗", 21);
        superBoss.getArtifactMap().put("番天印", 21);
        superBoss.getArtifactMap().put("卸甲金葫", 24);
        superBoss.getArtifactMap().put("定海珠", 6);
        superBoss.setAwardDropGroup(12);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL, Arrays.asList(11, 12, 13, 14, 15, 31, 32, 33, 34, 35));
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD, Arrays.asList(61, 62, 63, 64, 65, 81, 82, 83, 84, 85));
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER, Arrays.asList(110, 111, 112, 113, 114, 131, 132, 133, 134, 135));
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE, Arrays.asList(161, 162, 163, 164, 165, 181, 182, 183, 184, 185));
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH, Arrays.asList(210, 211, 212, 213, 214, 231, 232, 233, 234, 235));
        superBossMap.put(superBoss.getName(), superBoss);
    }

    @Override
    protected String getNpcContent(Role role, NPC bossNpc) {
        return null;
    }

    @Override
    protected String getNpcContentNotFight(Role role, NPC bossNpc) {

        /**检测队伍人数*/
        int teamCount = teamService.getTeamCount(role);
        if (teamCount < MIN_TEAM_COUNT) {
            return TEAM_COUNT_NOT_ENOUGH;
        }

        /**检测等级*/
        Team team = teamService.getTeam(role.getRoleId());
        String names = teamService.checkMember(team, memberRole -> memberRole.getLevel() < ACCEPT_MIN_LEVEL);
        if (names != null) {
            return MessageFormat.format(TEAM_LEVEL_NOT_ENOUGH, names, ACCEPT_MIN_LEVEL);
        }

        names = teamService.checkMember(team, memberRole -> getRoleRemainCount(memberRole) <= 0);
        if (names != null) {
            return "#R" + names + "#n没有挑战次数";
        }
        if (getRoleRemainCount(role) <= 0) {
            return "#R" + role.getName() + "#n没有挑战次数";
        }
        return null;
    }

    private int getRoleRemainCount(Role role) {
        return  SpringUtils.getActivityService().getRemainCount(role, ActivityType.lymy);
    }

    @Override
    protected void doStartFight(Role role, NPC bossNpc) {
        List<BossParam> bossParamList = newBossParamList(role, bossNpc);
        BossFightParam bossFightParam = new BossFightParam(bossParamList, getFightType(role));
        bossFightParam.setNpcId(bossNpc.getId());
        int maxRoleLevel = teamService.getMaxRoleLevelInTeam(role);
        bossFightParam.setNpcLevel((short) maxRoleLevel);
        bossService.startFightToBoss(role, bossFightParam);
    }

    public void challenge(Role role, NPC bossNpc){

        /**检测队伍人数*/
        int teamCount = teamService.getTeamCount(role);
        if (teamCount < MIN_TEAM_COUNT) {
            sendNpcContent(role, bossNpc, TEAM_COUNT_NOT_ENOUGH);
            return ;
        }

        /**检测等级*/
        Team team = teamService.getTeam(role.getRoleId());
        String names = teamService.checkMember(team, memberRole -> memberRole.getLevel() < ACCEPT_MIN_LEVEL);
        if (names != null) {
             MessageFormat.format(TEAM_LEVEL_NOT_ENOUGH, names, ACCEPT_MIN_LEVEL);
            return;
        }

        names = teamService.checkMember(team, memberRole -> getRoleRemainCount(memberRole) <= 0);
        if (names != null) {
            sendNpcContent(role, bossNpc, "#R" + names + "#n没有挑战次数");
            return ;
        }
        if (getRoleRemainCount(role) <= 0) {
            sendNpcContent(role, bossNpc,  "#R" + role.getName() + "#n没有挑战次数");
            return ;
        }
        List<BossParam> bossParamList = newBossParamList(role, bossNpc);
        BossFightParam bossFightParam = new BossFightParam(bossParamList, getFightType(role));
        bossFightParam.setNpcId(bossNpc.getId());
        int maxRoleLevel = teamService.getMaxRoleLevelInTeam(role);
        bossFightParam.setNpcLevel((short) maxRoleLevel);
        bossService.startFightToBoss(role, bossFightParam);
    }
    private List<BossParam> newBossParamList(Role role, NPC bossNpc) {
        bossNpc.setName("炼狱冥炎");
        SuperBoss superBoss = getSuperBoss(bossNpc.getName());
        List<BossParam> bossParamList = new ArrayList<>();
        int count = 10;
        SuperBossParam superBossParam = SpringUtils.getBean(BoosService.class).newSuperBossParam(bossNpc,getSuperBoss("炼狱冥炎"));
        superBossParam.setMainShowAfterRound(superBoss.getMainShowAfterRound());
        superBossParam.setSmallEscapeAfterRound(superBoss.getSmallEscapeAfterRound());
        if (superBoss.isSmallPolarRandom()) {
            byte polar = 2;
            superBossParam.setPolar(polar);
        }
        if (superBoss.isSmallSkillRandom()) {
            ArrayList<Integer> skillIds = new ArrayList<>(superBoss.getPolarSmallSkills().get((byte) superBossParam.getPolar()));
            superBossParam.setSkillIds(skillIds);
        }
        bossParamList.add(superBossParam);

        /**加count-1个*/
        int smallCount = superBoss.getZPName() != null ? count - 1 : count;
        for (int i = 1; i < smallCount; i++) {
            int index = (int) (Math.random() * boy_name.length);
            BossSet bossSet = bossService.getBossSet(boy_name[index]);
            BossParam bossParam = new BossParam(bossSet, bossSet.getName());
            if (superBoss.isSmallPolarRandom()) {
                byte polar = SpringUtils.getBean(BoosService.class).getRandomPolar();
                bossParam.setPolar(polar);
            }
            if (superBoss.isSmallSkillRandom()) {
                ArrayList<Integer> skillIds = new ArrayList<>(superBoss.getPolarSmallSkills().get((byte) bossParam.getPolar()));
                bossParam.setSkillIds(skillIds);
            }
            bossParamList.add(bossParam);
        }
        if (superBoss.getZPName() != null) {
            BossSet bossSet = bossService.getBossSet(superBoss.getZPName());
            bossParamList.add(new SuperBossZPParam(bossSet, bossSet.getName()));
        }

        return bossParamList;
    }



    private SuperBoss getSuperBoss(String name) {
        return superBossMap.get(name);
    }

    @Override
    protected int getFightType(Role role) {
        return Const.fightType_lymy;
    }

    @Override
    protected NPC getBossNpc(int npcId) {
        return SpringUtils.getMapService().getNpc(npcId);
    }

    @Override
    protected void clearNpcAfterWin(Role role, NPC bossNpc) {
        clearNpc(bossNpc);
    }

    private void clearNpc(NPC npc) {
        return;
    }

    @Override   //掉落奖励
    protected void giveReward(Role role, FightEndEvent fightEndEvent, NPC bossNpc) {
        // 这里改成公共获取配置 经验，道行，武学 奖励。 不再单独写
        fightService.sendBossBasicsReward(role,fightEndEvent);
        SpringUtils.getActivityService().addFinishCount(role, ActivityType.lymy, 1);
        //获取道具奖励
        fightService.sendBossExtReward(role,fightEndEvent);
    }

    @Override
    protected void clearNpcTimeOut(NPC npc) {
        clearNpc(npc);
    }

    @Override
    public void doFightFail(Role role, FightEndEvent fightEndEvent) {
        super.doFightFail(role, fightEndEvent);
        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getRoleService().punishFightDead(memberRole));
    }


    /**
     * 首杀BOOS奖励称号
     */
    @Override
    public void doFightWinForTeam(Team team, FightEndEvent fightEndEvent, NPC bossNpc) {
        String title = "炼狱冥炎·怒气冲天";
        final Boolean[] flag = {false};
        List<Map<String, String>> list = SpringUtils.getBean(BoosTitleDeal.class).getBossTitleInfo("lymy");
        teamService.memberHandleThreadLocal(team, role -> {
            /**发称号*/
            if (list.size() <= 0) {
                SpringUtils.getRoleService().addTitle(role, new TitleInfo(title, title));
                SpringUtils.getBean(BoosTitleDeal.class).addBoosTitle(role.getName(), "lymy", title);
                flag[0] = true;
            }
            giveReward(role, fightEndEvent, bossNpc);
        });
        // 更新数据库首次称号奖励
        if (flag[0]) {
            sendShouShaReward(team,bossNpc,title);
            SpringUtils.getBean(BoosTitleDeal.class).updateBoosTitle();
        }

    }
}
